﻿/*
创建人：zhangrj
日期：2023-02-28
描述：SPC历史处理措施维护页面js
*/
var page = {
    pageTotal: 0,
    pageSize: 10,
    pageNumber: 1,
    spcDealExperiencePoolID: "", //弹窗缓存处理记录ID
    ajaxUrl: getRootPath() + "/Ashx/StepCheckPointResultSPCHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        //列表加载
        $("#gdSpcExPoolInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pageTotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'OUTCONTROL', title: '失控原因', width: 240 },
                 { field: 'DEALMEASURE', title: '处理措施', width: 240 },
                 { field: 'FULLNAME', title: '创建人', width: 80 },
                 { field: 'CREATEDATE', title: '创建时间', width: 80, formatter: function (value, row, index) {
                     return dayjs(value).format("YYYY-MM-DD");
                 }
                 }
                ]],
            onClickRow: function (rowIndex, rowData) {
                $("#gdSpcExPoolInfo").datagrid("uncheckAll");
                $("#gdSpcExPoolInfo").datagrid("checkRow", rowIndex);
            }
        });
        //查询按钮
        $('#btnSearch').on("click", function () {
            page.pageNumber = 1;
            page.search();
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.search();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.clearPage(true, true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pageTotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
        //新增
        $("#btnAdd").on("click", function () {
            page.clearPage(false, false, true);
            page.openDialog(1);
        });
        //编辑
        $("#btnEdit").on("click", function () {
            page.clearPage(false, false, true);
            page.openDialog(2);
        });
        //删除
        $("#btnDele").on("click", function () {
            page.dele();
        });
        //保存
        $("#btnSave").on("click", function () {
            page.saveHistory();
        });
        //关闭
        $("#btnClose").on("click", function () {
            $("#dg").dialog("close");
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetSpcDealExperiencePool",
            pageSize: page.pageSize,
            pageIndex: page.pageNumber,
            experience: $("#txtExperience").textbox("getValue"),
            createName: $("#txtCreater").textbox("getValue"),
            createDate: $("#txtCreateDate").datebox("getValue")
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pageTotal = res.RowCount;
            $("#gdSpcExPoolInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //打开弹窗
    openDialog: function (type) {
        if (type === 2) {
            var selected = $("#gdSpcExPoolInfo").datagrid("getChecked");
            if (selected.length === 0) {
                displayMessage("请选择需要处理的数据", false);
                return false;
            }
            if (selected.length > 1) {
                displayMessage("请选择一条数据", false);
                return false;
            }
            page.spcDealExperiencePoolID = selected[0].SPCDEALEXPERIENCEPOOLID;
            $("#txtOutControl").textbox("setValue", selected[0].OUTCONTROL);
            $("#txtDealMeasure").textbox("setValue", selected[0].DEALMEASURE);
        }
        //打开弹窗
        $("#dg").dialog({
            onClose: function () { //关闭事件，恢复主页面按钮
                $("#mainDiv").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
        //禁用主页面所有按钮
        $("#mainDiv").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('disable');
        });
    },
    //存入历史经验
    saveHistory: function () {
        $("#btnSave").linkbutton('disable');
        $("#btnClose").linkbutton('disable');
        var outControl = $("#txtOutControl").textbox("getValue");
        var dealMeasure = $("#txtDealMeasure").textbox("getValue");
        if (isNullOrWhiteSpaceStr(outControl) || isNullOrWhiteSpaceStr(dealMeasure)) {
            displayMessage2("请完整填写数据", false, ["btnSave", "btnClose"]);
            return;
        }
        var formData = {
            action: "SaveSpcHistoryExperienceData",
            warnReason: outControl,
            solution: dealMeasure,
            spcDealExperiencePoolID: page.spcDealExperiencePoolID
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (res.Result) {
                page.search();
                $("#dg").dialog("close");
            }
            displayMessage2(res.Message, res.Result, ["btnSave", "btnClose"]);
        });
    },
    //删除历史经验
    dele: function () {
        $("#btnDele").linkbutton('disable');
        var selected = $("#gdSpcExPoolInfo").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage2("请选择需要处理的数据", false, ["btnDele"]);
            return false;
        }
        var formData = {
            action: "DeleSpcHistoryExperienceData",
            paramDt: JSON.stringify(selected)
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnDele"]);
        });
    },
    //清除页面（查询区域，列表区域，是否清空消息）
    clearPage: function (query, list, msg) {
        if (query) {
            $("#txtExperience").textbox("clear");
        }
        if (list) {
            clearEasyuiDataGrid("gdSpcExPoolInfo"); //清空列表
        }
        if (msg) {
            clearMessage();
        }
        $("#txtOutControl").textbox("setValue", "");
        $("#txtDealMeasure").textbox("setValue", "");
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
$(function () {
    page.init();
});